<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加音频</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">音频名称</label>
        <div class="layui-input-block">
            <input type="text" id="title" name="title" lay-verify="required" lay-reqtext="音频名称不能为空"
                   placeholder="请输入音频名称" value="" class="layui-input">
            <tip>填写音频的名称。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">音频地址</label>
        <div class="layui-input-block">
            <input id="url" name="url" type="text" class="layui-input" placeholder="请输入音频地址"></input>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所在ASMR</label>
        <div class="layui-input-block">
            <select id="asmrId" name="asmrId">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>


    layui.use(['form'], function () {
        const form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        $.ajaxSetup({
            beforeSend: function (xhr) {
// 假设你的 token 存储在 localStorage 中
                var token = localStorage.getItem('token');
                if (token) {
// 将 token 添加到请求头中
                    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
                }
            }
        });
//音频名称
        function bindselect(id, url, ts) {
            $.ajax({
                url: url,//访问的链接
                type: 'get',//提交方式 常用的是get或者post
                dataType: "json",//返回的数据类型
                async: false,//是否异步加载
                success: function (res) {//访问成功返回的方法
                    const data = res.data;
                    let html = '<option value="">' + ts + '</option>';
                    for (const row of data) {
                        html += '<option value="' + row['first'] + '">' + row['second'] + '</option>';
                    }
                    $("#" + id).html(html);
                    form.render(); //更新全部

                },
                error: function (err) {//访问失败返回的方法
                    //  debugger
                }
            })
        }

        bindselect("asmrId", "/api/admin/asmr?action=titleList", "请选择ASMR名称 ")


        const queryString = window.location.search;
        const queryParams = new URLSearchParams(queryString);
        const id = queryParams.get('id');
        if (id) {//编辑
            $("#title").val(queryParams.get('title'))//设置id为name的数据框的value属性为xxxx
            $("#asmrId").val(queryParams.get('asmrTitle'))
            $("#url").val(queryParams.get('url'))
            form.render(); //更新全部
        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let url = "/api/admin/audio?action=add&title=" + data.field.title + "&url=" + data.field.url + "&asmrId=" + data.field.asmrId;//添加
            if (id) {//编辑
                url = "/api/admin/audio?action=edit&id=" + id + "&title=" + data.field.title + "&url=" + data.field.url + "&asmrId=" + data.field.asmrId

            }

            $.ajax({
                url: url,//访问的链接
                type: 'get',//提交方式 常用的是get或者post
                dataType: "json",//返回的数据类型
                // async: false,//是否异步加载
                success: function (res) {//访问成功返回的方法
                    // debugger
                    layer.msg(res.message);
                    setTimeout(function () {
                        //执行搜索重载
                        parent.layui.table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                        }, 'data');
                        const iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);

                    }, 1000)

                },
                error: function (err) {//访问失败返回的方法
                    //  debugger
                }
            })

            return false;
        });

    });
</script>
</body>
</html>
